/*
 * Copyright (C) 2018-2021 The Project Lombok Authors.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package p.ithorns.tools.translator.apt.utils;

import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.comp.*;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Context;

import javax.lang.model.element.Element;
import java.lang.reflect.Method;

/**
 * EnterReflect
 *
 * @author lombok
 */
public class EnterReflect {

    private static final Method CLASS_ENTER;
    private static final Method MEMBER_ENTER;
    private static final Method BLOCK_ANNOTATIONS;
    private static final Method UNBLOCK_ANNOTATIONS;

    static {
        CLASS_ENTER = Permit.permissiveGetMethod(Enter.class, "classEnter", JCTree.class, Env.class);
        MEMBER_ENTER = Permit.permissiveGetMethod(MemberEnter.class, "memberEnter", JCTree.class, Env.class);

        Method block = Permit.permissiveGetMethod(Annotate.class, "blockAnnotations");
        if (block == null) {
            block = Permit.permissiveGetMethod(Annotate.class, "enterStart");
        }
        BLOCK_ANNOTATIONS = block;

        Method unblock = Permit.permissiveGetMethod(Annotate.class, "unblockAnnotations");
        if (unblock == null) {
            unblock = Permit.permissiveGetMethod(Annotate.class, "enterDone");
        }
        UNBLOCK_ANNOTATIONS = unblock;
    }

    public static Type classEnter(Context context, JCTree tree, JCTree parent) {
        Enter enter = Enter.instance(context);
        Env<AttrContext> classEnv = enter.getEnv((com.sun.tools.javac.code.Symbol.TypeSymbol) getElement(parent));
        if (classEnv == null) {
            return null;
        }
        Type type = (Type) Permit.invokeSneaky(CLASS_ENTER, enter, tree, classEnv);
        if (type == null) {
            return null;
        }
        type.complete();
        return type;
    }

    public static void memberEnter(Context context, JCTree node, JCTree parent) {
        MemberEnter me = MemberEnter.instance(context);
        Annotate annotate = Annotate.instance(context);
        Enter enter = Enter.instance(context);

        Env<AttrContext> classEnv = enter.getEnv((com.sun.tools.javac.code.Symbol.TypeSymbol) getElement(parent));
        if (classEnv == null) {
            return;
        }

        Permit.invokeSneaky(BLOCK_ANNOTATIONS, annotate);
        Permit.invokeSneaky(MEMBER_ENTER, me, node, classEnv);
        Permit.invokeSneaky(UNBLOCK_ANNOTATIONS, annotate);
    }

    static Element getElement(JCTree node) {
        if (node instanceof JCTree.JCClassDecl) {
            return ((JCTree.JCClassDecl) node).sym;
        }
        if (node instanceof JCTree.JCMethodDecl) {
            return ((JCTree.JCMethodDecl) node).sym;
        }
        if (node instanceof JCTree.JCVariableDecl) {
            return ((JCTree.JCVariableDecl) node).sym;
        }
        return null;
    }

}